package com.app.micaihu.view.newsdetail;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.PariseBean;
import com.app.micaihu.bean.comment.PariseList;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.view.newsdetail.adapter.PariseAdapter;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论点赞的列表
 * Created by linkai on 2016/9/8.
 */
public class CommentPariseActivity extends BaseListActivity<PariseBean> {

    //逻辑数据
    private String articleId;//文章id
    private String commentId;//评论id

    private TextView footerView;
    private HashMap<String, String> listHasp = new HashMap<String, String>();
    private String notice;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            articleId = intent.getStringExtra(Config.Extra.PARAMETER1);
            commentId = intent.getStringExtra(Config.Extra.PARAMETER2);
        }
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void changeView() {
        super.changeView();
        setTitle("赞过的人");
        footerView = (TextView) getLayoutInflater().inflate(R.layout.layout_text_center, null);
        footerView.setClickable(false);
        mListView.getRefreshableView().addFooterView(footerView);
    }


    @Override
    protected void changeData(final boolean isRefresh) {
        final Map<String, String> params = new HashMap<>();
        params.put("articleId", articleId);
        params.put("commentId", commentId);
        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        } else {
            params.put("uid", "");
        }
        params.put("page", dataPage + "");
        HttpListener<DataBean<PariseList>> topicsListener = new HttpListener<DataBean<PariseList>>() {


            @Override
            public void onStart() {
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override

            public void onSuccess(DataBean<PariseList> response) {
                if (response.noError() && response.getData() != null) {
                    PariseList pariseList = response.getData();
                    List<PariseBean> pariseBeanList = pariseList.getPraiseList();

                    notice = pariseList.getNotice();
                    if (pariseBeanList != null && pariseBeanList.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<>();
                        }
                        if (isRefresh) {
                            dataList.clear();
                            listHasp.clear();
                        }

                        for (int i = 0; i < pariseBeanList.size(); i++) {
                            //去除黑名单
                            if (InteractiveUtils.getInstance().theDefriendIsContains(pariseBeanList.get(i).getUid())) {
                                pariseBeanList.remove(pariseBeanList.get(i));
                                i--;
                            }
                            //开始去重计算
                            if (listHasp.get(pariseBeanList.get(i).getUid()) == null) {
                                listHasp.put(pariseBeanList.get(i).getUid(), pariseBeanList.get(i).getUid());
                            } else {
                                pariseBeanList.remove(i);
                                i--;
                            }
                        }
                        dataList.addAll(pariseBeanList);
                        if (mAdapter == null) {
                            mAdapter = new PariseAdapter(dataList, mActivity);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);

                    } else if (dataList == null || dataList.isEmpty()) {
                        setLoadView(LOADSUCES, AppApplication.getInstance().getString(R.string.pullup_no_data));
                    } else {
                        setLoadView(LOADSUCES, "没有数据了");
                    }
                    if (TextUtils.isEmpty(notice)) {
                        footerView.setVisibility(View.GONE);
                    } else {
                        footerView.setText(notice);
                        footerView.setVisibility(View.VISIBLE);
                    }
                } else {
                    //updateLoadView(DATAERROR, response.getNnderstoodMsg());
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };

        addPostCacheRequest(URIContant.PARISELIST, new TypeToken<DataBean<PariseList>>() {
        }.getType(), params, topicsListener);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if (position >= dataList.size()) {
            return;
        }
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0
                || mActivity == null
                || dataList.get(position) == null) {
            return;
        }
        Intent intent = new Intent(mActivity, MyHomepageDynamicActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, dataList.get(position).getUid());
        mActivity.startActivity(intent);
    }


}
